#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    //实例化界面
      myLogin= new MyLogin;
      myHomePage= new MyHomePage;
      stmshow= new STMShow;
      modbusshow=new ModbusShow;
      movieshow=new MovieShow;
      historyshow=new HistoryShow;
    //将界面依次添加到堆栈窗体
      ui->stackedWidget->addWidget(myLogin);
      ui->stackedWidget->addWidget(myHomePage);
      ui->stackedWidget->addWidget(stmshow);
      ui->stackedWidget->addWidget(modbusshow);
      ui->stackedWidget->addWidget(movieshow);
      ui->stackedWidget->addWidget(historyshow);
    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.31.229"));
    mqtt->setPort(1883);
    mqtt->connectToHost();

    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     myHomePage, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     stmshow, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     modbusshow, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     movieshow, SLOT(dataUpSlot(QByteArray)));
}

HomePage::~HomePage()
{
    delete ui;
}
